﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using LightSmokeTemperatureCameraAdjust.Properties;

namespace LightSmokeTemperatureCameraAdjust
{
	internal class ElementNeedMoveAnalyzer
	{
		public List<Element> LstElement
		{
			get
			{
				return this.m_lstElement;
			}
			private set
			{
				this.m_lstElement = value;
			}
		}

		public void GetElements(UIDocument uiDoc, List<BuiltInCategory> lstStrCategory, int index, List<ElementId> lstViewId = null)
		{
			if (index == 0)
			{
				using (List<ElementId>.Enumerator enumerator = lstViewId.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ElementId idLevel = enumerator.Current;
						this.LstElement.AddRange(this.GetElements(uiDoc.Document, lstStrCategory, idLevel));
					}
					return;
				}
			}
			if (index == 1)
			{
				this.LstElement.AddRange(this.GetElements(uiDoc, lstStrCategory));
				if (this.LstElement.IsEmpty<Element>())
				{
					YJKMessageBox.Show("没有框选到吸顶设备。");
				}
			}
		}

		private List<Element> GetElements(Document doc, List<BuiltInCategory> lstStrCategory, ElementId idLevel)
		{
			List<Element> list = new List<Element>();
			foreach (BuiltInCategory builtInCategory in lstStrCategory)
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				filteredElementCollector.OfClass(typeof(FamilyInstance)).OfCategory(builtInCategory);
				foreach (Element element in filteredElementCollector)
				{
					if (element.LevelId.Equals(idLevel))
					{
						string familyName = (element as FamilyInstance).Symbol.FamilyName;
						if (familyName.Contains(Resources.StrLight) || familyName.Contains(Resources.StrFireDetectors) || familyName.Contains(Resources.StrDomeCamera))
						{
							list.Add(element);
						}
					}
				}
			}
			return list;
		}

		private List<Element> GetElements(UIDocument uiDoc, List<BuiltInCategory> lstStrCategory)
		{
			new List<Element>();
			this.CategoryTransToId(lstStrCategory);
			ElementTypeFilter elementTypeFilter = new ElementTypeFilter(lstStrCategory);
			return (from n in uiDoc.Selection.PickElementsByRectangle(elementTypeFilter, "请框选……")
			select n).ToList<Element>();
		}

		private List<int> CategoryTransToId(List<BuiltInCategory> lstStrCategory)
		{
			List<int> list = new List<int>();
			foreach (BuiltInCategory item in lstStrCategory)
			{
                list.Add((int)item);
			}
			return list;
		}

		private List<Element> m_lstElement = new List<Element>();
	}
}
